/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.util;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.mail.*;

/**
 *
 * @author roniere
 */
@ManagedBean
@SessionScoped
public class NotarioMail implements Serializable{
    
    private String HOST;
    private String PROTOCOLO;
    private String FOLDER;
    
    private String msg;

    public NotarioMail() {
        super();
        HOST = "imap.gmail.com";
        PROTOCOLO = "imaps";
        FOLDER = "INBOX";
    }
    
    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
    
       

    public void init() throws MessagingException {
        verEmails("ronieresk8@gmail.com","94293212");
    }

    public void verEmails(String login, String senha) throws MessagingException {
        try{
            Session session = Session.getInstance(System.getProperties());
            
            Store store = session.getStore(PROTOCOLO);
            store.connect(HOST, login, senha);
            Folder folder = store.getFolder(FOLDER);
            folder.open(Folder.READ_ONLY);

            Message message[] = folder.getMessages();

            System.err.println("Mensagens não lidas"+folder.getUnreadMessageCount());

            msg = ""+folder.getUnreadMessageCount();

//          for (int i = message.length - 1; i > 0  ; i--) {            
//            if (!message[i].isSet(Flags.Flag.SEEN))  
//                 System.err.println(i+""+message[i].getFrom()[0] + message[i].getSentDate());
//            }
          
            folder.close(false);
            store.close();
        }catch(Exception e){
             System.err.println("ERRO !");
             msg = "0";
        }
        
    }
    
      
}
